<?php

declare(strict_types=1);

namespace App\Controller\Chat;

use App\Constants\CodeData;
use App\Controller\BaseController;
use App\Exception\MessageException;
use App\Request\Chat\JoinExitMicroRequest;
use App\Request\Chat\UserJoinExitRoomRequest;
use App\Services\Chat\ChatUserService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class UserController extends BaseController
{
    #[Inject]
    protected ChatUserService $chatUserService;

    /**
     * Note - 获取进入聊天室的用户信息
     * DateTime 2024/9/3 18:36
     * User Admin
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function user()
    {
        $params = $this->request->post();
        $data = $this->chatUserService->getUserInfo($params['uid']);
        return $this->return($data);
    }


    /**
     * Note - 获取房间内用户列表
     * DateTime 2024/9/5 10:40
     * User Admin
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getUserList()
    {
        $params = $this->request->post();
        $page = $params['page'] ?? 1;
        $pageSize = $params['page_size'] ?? 20;
        $data['msg'] = $this->chatUserService->getInRoomUserList($params['room_number'], $page, $pageSize);
        $data['result'] = 1;
        return $this->return(formatData($data));
    }

    /**
     * Note - 用户加入房间
     * DateTime 2024/9/3 18:36
     * User Admin
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function userJoinRoom(UserJoinExitRoomRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $this->chatUserService->userJoinRoom($params['uid'], $params['room_number']);
        return $this->return();

    }

    /**
     * Note - 用户退出房间
     * DateTime 2024/9/11 12:22
     * User Admin
     * @param UserJoinExitRoomRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function userExitRoom(UserJoinExitRoomRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $this->chatUserService->userExitRoom($params['uid'], $params['room_number']);
        return $this->return();

    }


    /**
     * Note - 用户加入\退出麦克风
     * DateTime 2024/9/4 17:29
     * User Admin
     * @param JoinExitMicroRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function joinExitMicro(JoinExitMicroRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        try {
            Db::beginTransaction();
            $this->chatUserService->joinExitMicro($params);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollBack();
            logInfo('joinExitMicro', $e->getMessage());
            if ($e instanceof MessageException) {
                throw new MessageException($e->getMessage(), CodeData::NEED_TIPS, [], false);
            }
            throw new MessageException('麦克风操作失败');
        }
        return $this->return();

    }

}
